let flag = 'getComputedStyle' in window; //flag这个变量不销毁，存储的是判断当前浏览器是否兼容getComputedStyle，兼容的话是标准浏览器，false是ie6-8
const Utils = {
    /**
     * 获取url的参数值
     * @param name:参数名
     * */
    get_url_param: (name) => {
        let query = window.location.search.substring(1); //?后面的值
        let paramList = query.split('&');
        for (let i = 0; i < paramList.length; i++) {
            let pair = paramList[i].split('=');
            if (pair[0] === name) {
                return pair[1];
            }
        }
        return false;
    },

    // 补0
    format_number: (n) => {
        n = n < 10 ? '0' + n : n;
        return n.toString();
    },

    /**
     * 转换时间
     * @param time:时间戳
     * @param type:年月份格式
     * @param type2:时分秒格式
     * */
    format_time: (time, type, type2) => {
        const date = new Date(time);
        const year = date.getFullYear();
        const mounth = date.getMonth() + 1;
        const day = date.getDate();
        const hour = date.getHours();
        const minute = date.getMinutes();
        const second = date.getSeconds();
        return [year, mounth, day].map(Utils.format_number).join(type || '/') + ' ' + [hour, minute, second].map(Utils.format_number).join(type2 || ':');
    },

    /**
     * 转换时间（需传入格式）
     * @param time: 传入时间
     * @param type: 传入格式
     * format_date(new Date(1409894060000), 'yyyy-MM-dd HH:mm:ss 星期w') => "2014-09-05 13:14:20 星期五"
    */
   format_date(time,type){
        const date = new Date(time);
        const obj = {
            'yyyy': date.getFullYear(),
            'yy': (''+ date.getFullYear).slice(-2),
            'MM': ('0'+date.getMonth()+1).slice(-2),
            'M': time.getMonth()+1,
            'dd': ('0'+ (time.getDate())).substr(-2),
            'd': time.getDate(),
            'HH': ('0'+ (time.getHours())).substr(-2),
            'H':  time.getHours(),
            'hh': ("0"+ time.getHours() % 12).substr(-2),
            'h': time.getHours() % 12,
            'mm': ('0'+ (time.getMinutes())).substr(-2),
            'm': time.getMinutes(),
            'ss': ('0'+ (time.getSeconds())).substr(-2),
            's': time.getSeconds(),
            'w': function(){
                var day = ['日', '一', '二', '三', '四', '五', '六'];
                return day[time.getDay()]
            }()
        }
        for(const i in obj){
        type =  type.replace(i,obj[i])
        }
        return type;
   },

    /**
     * 检测是否在微信
     * */
    is_weixn() {
        const ua = navigator.userAgent.toLowerCase();
        if (ua.match(/MicroMessenger/i) === 'micromessenger') {
            alert('在微信打开');
        } else {
            alert('不在微信打开');
        }
    },

    /**
     * 检测手机类型
     **/
    is_iphone_type() {
        const ua = navigator.userAgent;
        const isAndroid = ua.indexOf('Android') > -1 || ua.indexOf('Adr') > -1; //android终端
        const isiOS = !!ua.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
        if (isAndroid) {
            alert('Android');
        }
        if (isiOS) {
            alert('IOS');
        }
    },

    /**
     * 检测浏览器类型
     * */
    system_navigat() {
        const Sys = { browser: 'Unknown' };
        const ua = navigator.userAgent.toLowerCase();
        if (ua.match(/firefox\/([\d.]+)/)) {
            Sys.browser = 'Firefox';
        } else if (ua.match(/chrome\/([\d.]+)/)) {
            Sys.browser = 'Chrome';
        } else if (ua.match(/opera\/([\d.]+)/)) {
            Sys.browser = 'Opera';
        } else if (ua.match(/version\/([\d.]+).*safari/)) {
            Sys.browser = 'Safari';
        }
        return Sys.browser;
    },

    /**
     * 字符串替换
     * @param str:字符串
     * @param beforeText: 要替换的字符串
     * @param afterText: 替换后的字符串
     * */
    replace_all(str, beforeText, afterText) {
        var raRegExp = new RegExp(beforeText, 'g');
        return str.replace(raRegExp, afterText);
    },

    /**
     * 数组去重
     * */
    unique(arr) {
        if (!flag) {
            return Array.form(new Set(arr));
        } else {
            let array = [];
            for (let i = 0; i < arr.length; i++) {
                if (array.indexOf(arr[i]) === -1) {
                    array.push(arr[i]);
                }
            }
            return array;
        }
    },

    /**
     * 数组扁平化
     * */
    flatten(arr) {
        if (!flag) {
            return arr.flat(Infinity);
        } else {
            return arr.reduce((acc, cur) => {
                Array.isArray(cur) ? [...acc, ...this.flatten(cur)] : [...acc, cur];
            });
        }
    },
    urlFormat(path) {
        return path ? process.env.VUE_APP_IMA + path + '?v=' + process.env.VUE_APP_API_V: '';
    },
    async downloadRes(url,name) {
        const response = await fetch(Utils.urlFormat(url)); // 内容转变成blob地址
        const blob = await response.blob(); // 创建隐藏的可下载链接
        const objectUrl = window.URL.createObjectURL(blob);
        const a = document.createElement('a');
        a.href = objectUrl;
        a.download = name + '.xls';
        a.click();
        a.remove();
    }
    

};
export default Utils;
